package com.dmap.community.controller.app;

import com.dmap.base.vo.BaseVo;
import com.dmap.community.bo.theme.ShowThemeBo;
import com.dmap.community.constant.Constant;
import com.dmap.community.controller.app.param.HistoryThemeParam;
import com.dmap.community.controller.app.param.SelectThemeParam;
import com.dmap.community.controller.app.param.ThemeParam;
import com.dmap.community.service.community.ThemeService;
import com.dmap.community.vo.NormalResponseVo;
import com.dmap.community.vo.theme.ShowThemeVo;
import com.dmap.community.vo.theme.ThemeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 14/11/17 Time: 14:25
 * \* Description:
 * \
 */
@Api(value = "App/社区操作API", description = "App/社区操作API")
@RestController
@RequestMapping(value = "/api/dmap-community-server/v1/community/theme")
public class ThemeController {
    @Resource
    private ThemeService themeService;


    @ResponseBody
    @ApiOperation(value = "新增社区", notes = "新增社区")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertTheme(@RequestBody ThemeParam param) {
        BaseVo result = new BaseVo();
        if (param.getType() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = themeService.insertTheme(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查询社区", notes = "查询社区")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public ShowThemeVo selectThemeByType(SelectThemeParam param) {
        ShowThemeVo result = new ShowThemeVo();
        if (param.getType() == null || param.getPageIndex() == null || param.getPageSize() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        List<ShowThemeBo> showThemeBos = themeService.showThemeListByType(param.getType(), param.getPageIndex(), param.getPageSize());
        result.setData(showThemeBos);
        return result;
    }


    @ResponseBody
    @ApiOperation(value = "查询个人历史发布的", notes = "查询个人历史发布的")
    @RequestMapping(value = "/history/select", method = RequestMethod.POST)
    public ShowThemeVo selectThemeByUserId(@RequestBody HistoryThemeParam param) {
        ShowThemeVo result = new ShowThemeVo();
        if (param.getUserId() == null || param.getPageIndex() == null || param.getPageSize() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        List<ShowThemeBo> showThemeBos = themeService.showThemeListByUserId(param.getUserId(), param.getPageIndex(), param.getPageSize());
        result.setData(showThemeBos);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "删除社区", notes = "删除社区")
    @RequestMapping(value = "/delete/{themeId}", method = RequestMethod.DELETE)
    public BaseVo deleteTheme(@PathVariable(value = "themeId") long themeId) {
        BaseVo result = new BaseVo();
        if (themeId == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
        }
        int success = themeService.deleteTheme(themeId);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
        }
        return result;
    }


    @ResponseBody
    @ApiOperation(value = "根据Id查询社区", notes = "根据Id查询社区")
    @RequestMapping(value = "/select/{themId}", method = RequestMethod.POST)
    public ThemeVo selectThemeById(@PathVariable(value = "themId") long themId) {
        ThemeVo result = new ThemeVo();
        if (themId == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        ShowThemeBo showThemeBo = themeService.selectThemeByThemeId(themId);
        result.setShowThemeBo(showThemeBo);
        return result;
    }


}
